package com.jingdun.sport.common.util;

import com.alibaba.fastjson.JSONObject;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class kuaidi100 {

    public static void main(String[] args) {
        /** 请求快递100返回的格式
         * [{
         * 	"ftime": "2020-02-20 18:02:37",
         * 	"context": "[杭州拱墅美都广场营业部]已签收,感谢使用顺丰,期待再次为您服务（主单总件数：1件）",
         * 	"time": "2020-02-20 18:02:37"
         * }, {
         * 	"ftime": "2020-02-20 15:35:03",
         * 	"context": "[美都广场速运营业点]快件派送不成功(已与收方客户约定新派送时间 2020-03-01 08:34:02),待再次派送",
         * 	"time": "2020-02-20 15:35:03"
         * }, {
         * 	"ftime": "2020-02-20 11:55:06",
         * 	"context": "[美都广场速运营业点]派件员从【丰巢智能快递柜】取出快件,取件码已失效(派件人:戈振强,电话:18720656516)",
         * 	"time": "2020-02-20 11:55:06"
         * }, {
         * 	"ftime": "2020-02-18 10:40:16",
         * 	"context": "[美都广场速运营业点]快件派送至【丰巢智能快递柜】,请您凭取件码至翠苑一区东大门保安岗亭旁丰巢智能柜领取您的快递.",
         * 	"time": "2020-02-18 10:40:16"
         * }, {
         * 	"ftime": "2020-02-18 08:26:08",
         * 	"context": "[美都广场速运营业点]快件交给戈振强,正在派送途中（联系电话：18720656516,顺丰已开启“安全呼叫”保护您的电话隐私,请放心接听！）",
         * 	"time": "2020-02-18 08:26:08"
         * }, {
         * 	"ftime": "2020-02-18 06:32:40",
         * 	"context": "[美都广场速运营业点]快件到达 【杭州拱墅美都广场营业部】",
         * 	"time": "2020-02-18 06:32:40"
         * }, {
         * 	"ftime": "2020-02-18 05:15:11",
         * 	"context": "[杭州下沙中转场]快件已发车",
         * 	"time": "2020-02-18 05:15:11"
         * }, {
         * 	"ftime": "2020-02-18 05:14:59",
         * 	"context": "快件在【杭州下沙中转场】已装车,准备发往 【杭州拱墅美都广场营业部】",
         * 	"time": "2020-02-18 05:14:59"
         * }, {
         * 	"ftime": "2020-02-17 21:48:12",
         * 	"context": "快件到达 【杭州下沙中转场】",
         * 	"time": "2020-02-17 21:48:12"
         * }, {
         * 	"ftime": "2020-02-17 20:25:22",
         * 	"context": "[嘉兴南湖中转场]快件已发车",
         * 	"time": "2020-02-17 20:25:22"
         * }, {
         * 	"ftime": "2020-02-17 20:19:58",
         * 	"context": "快件在【嘉兴南湖中转场】已装车,准备发往 【杭州下沙中转场】",
         * 	"time": "2020-02-17 20:19:58"
         * }, {
         * 	"ftime": "2020-02-17 19:25:16",
         * 	"context": "快件到达 【嘉兴南湖中转场】",
         * 	"time": "2020-02-17 19:25:16"
         * }, {
         * 	"ftime": "2020-02-17 18:37:43",
         * 	"context": "[昌盛北路速运营业点]快件已发车",
         * 	"time": "2020-02-17 18:37:43"
         * }, {
         * 	"ftime": "2020-02-17 18:37:34",
         * 	"context": "[昌盛北路速运营业点]快件在【嘉兴秀洲昌盛北路营业点】已装车,准备发往 【嘉兴南湖中转场】",
         * 	"time": "2020-02-17 18:37:34"
         * }, {
         * 	"ftime": "2020-02-17 17:24:47",
         * 	"context": "[昌盛北路速运营业点]顺丰速运 已收取快件",
         * 	"time": "2020-02-17 17:24:47"
         * }, {
         * 	"ftime": "2020-02-17 17:14:28",
         * 	"context": "[昌盛北路速运营业点]顺丰速运 已收取快件",
         * 	"time": "2020-02-17 17:14:28"
         * }]
         */
        System.out.println(kuaidi100.rightKuaidi100("SF1017621228146", "shunfeng"));
    }

    /**
     * 调用快递100接口，查询物流信息
     *
     * @param number          要查询的快递单号
     * @param expressCategory 快递单号所对应的快递公司的编码，比如 “yuantong”
     * @return
     */
    public static List<Map<String, Object>> rightKuaidi100(String number, String expressCategory) {
        //此处根据快递单号拿到对应的公司的编码，比如 “yuantong”,这部分代码我就不上了
        //String expressCategory = kuaidi100Service.getExpressCategory(number);

        String param = "{\"com\": \"" + expressCategory + "\", \"num\": \"" + number + "\"}";
        String key = "alPlwKXk5641";
        String customer = "E2B9B304BCA0D4947ACC644EDEA90E67";
        String sign = signMD5(param + key + customer);
        HashMap params = new HashMap();
        params.put("param", param);
        params.put("sign", sign.toUpperCase());
        //params.put("sign",sign);
        params.put("customer", customer);

        String responseString = OkHttp3Utils.okHttpPostString("http://poll.kuaidi100.com/poll/query.do", params);
        //格式化返回参数信息
        Map<String, Object> map1 = (Map<String, Object>) JSONObject.parse(responseString);
        return (List<Map<String, Object>>) map1.get("data");
    }

    /**
     * 快递100 签名
     *
     * @param s
     * @return
     */
    public static String signMD5(String s) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest(s.getBytes("utf-8"));
            return bytesToHex(bytes);
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    public static String bytesToHex(byte[] bytes) {
        char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
        StringBuilder ret = new StringBuilder(bytes.length * 2);

        for (int i = 0; i < bytes.length; ++i) {
            ret.append(HEX_DIGITS[bytes[i] >> 4 & 15]);
            ret.append(HEX_DIGITS[bytes[i] & 15]);
        }

        return ret.toString();
    }
}